﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Web;
using Cms.Entity.Article;
using Cms.BLL.Menu;
using Cms.Entity.Menu;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Cms.BLL.Article;

namespace Cms.Admin.Article
{
    public partial class List : PageList
    {
        private ArticleBO Article
        {
            get
            {
                return ArticleBO.Instance<ArticleBO>();
            }
        }

        private MenuBO Menu
        {
            get
            {
                return MenuBO.Instance<MenuBO>();
            }
        }


        private string strOrders;

        private string _menuID;
        private string MenuID
        {
            get
            {
                if (_menuID == null)
                {
                    if (string.IsNullOrEmpty(Request["MenuID"]))
                    {
                        _menuID = Menu.GetIDByCode(MenuCode).ToString();
                    }
                    else
                    {
                        _menuID = StrUtil.DecryptUrl(Request["MenuID"]);
                    }
                }
                return _menuID;
            }
        }


        private string MenuCode
        {
            get
            {
                return Request["MenuCode"];
            }
        }

        private string PageSize
        {
            get
            {
                return Request.QueryString["PageSize"];
            }
        }

        private string ArticleName
        {
            get
            {
                return Request.QueryString["ArticleName"];
            }
        }

       // private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
           // _setting = this.Footer1.InitPager(new BindViewHandler(BindView));
           
            if (!IsPostBack)
            {
                MenuBO menu = new MenuBO();
                MenuInfo menuInfo = menu.GetInfo(MenuID);

                switch ((MenuType)menuInfo.TypeID)
                {
                    case MenuType.图片:
                        Response.Redirect("../picture/PictureList.aspx?MenuID=" + StrUtil.EncryptUrl(this.MenuID), true);
                        break;
                    case MenuType.图片组:
                        Response.Redirect("../picture/GroupList.aspx?MenuID=" + StrUtil.EncryptUrl(this.MenuID), true);
                        break;
                    case MenuType.附件:
                        Response.Redirect("../Attach/List.aspx?MenuID=" + StrUtil.EncryptUrl(this.MenuID), true);
                        break;
                    case MenuType.单篇文章:
                        List<ArticleInfo> list = Article.GetDisplayList(menuInfo.ID);
                        string url = null;
                        if (list.Count > 0)
                        {
                            url = "Edit.aspx?ID=" + this.Encrypt(list.First().ID);
                        }
                        else
                        {
                            url = "Edit.aspx?MenuID=" + MenuID;
                        }
                        Response.Redirect(url, true);
                        break;
                }
             
                Pager.BindDrp(drpPager);

                if (!string.IsNullOrEmpty(this.PageSize))
                {
                    drpPager.SelectedValue = this.PageSize;
                }

                txtSearch.Text = this.ArticleName;

                BindView();
                
                lblMenuName.Visible = true;
                lblMenuName.Text = menuInfo.Name + "&nbsp;";
                
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "文章");

            string url = "Edit.aspx";
            if (MenuID != null)
            {
                url += "?MenuID=" + MenuID;
            }

            lnkAdd.HRef = "Edit.aspx?MenuID=" + MenuID;
            btnMove.Attributes.Add("onclick", "moveDept();");
            JSUtil.ClickBtn(lnkMove, btnMove);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
           
        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            chkSelectAll.Checked = false;

            //_setting.Input = txtSearch.Text.Trim();
            //_setting.SortExpression = "Orders";
            //_setting.SortDirection = Core.Common.SortDirection.desc;

           // Pager.SetPageSize(_setting, drpPager);

            PagerSetting setting = new PagerSetting();
            setting.PageSize = int.Parse(drpPager.SelectedValue);
            setting.PageIndex = this.PageIndex;

            IEnumerable<ArticleInfo> list;

            MenuBO menu = new MenuBO();
            MenuInfo info = menu.GetInfo(MenuID);
            if (info.UpperID ==Constants.TREE_ROOT_UPPER_ID)
            {
                if (txtSearch.Text.Trim() != "")
                {
                    setting.Where = "Name like @p0";
                }
                setting.OrderBy = "Orders desc";
                list = Article.GetList(setting, "%" + txtSearch.Text.Trim() + "%");            
            }
            else
            {
                list = Article.GetList(int.Parse(MenuID), setting, txtSearch.Text.Trim());
            }

            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + setting.RecordCount + "</b>&nbsp;篇文章&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID"};
            lv.DataBind();

            string prefix = GlobalVar.WebCurrPath + "List.aspx?PageSize=" + drpPager.SelectedValue + "&MenuID=" + StrUtil.EncryptUrl(MenuID) + "&ArticleName=" + txtSearch.Text.Trim();

            litPager.Text = HtmlPager.Render(setting, prefix, true);

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);

            //要删除相关的图片和附件
            Article.Delete(lstSelectIDs);
            BindView();
        }

        protected void btnSaveSequence_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            Article.SaveOrders(ids, orders);
            BindView();
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }
       

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                lnkName.NavigateUrl = lnkEdit.NavigateUrl = "Edit.aspx?ID=" + strID;
               
            }
        }
    }
}
